package org.app.original.improve;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


import org.app.Ports;
import org.app.MyUtils;
import org.app.data.MyBean;
import org.app.entities.MyCoordinate;
import org.app.entities.Users;

public class MyFormat {
	//分隔符
//	Map types=new HashMap();
	MyDecrypt decrypt;
	String JSON="JSON",XML="XML";
	String format=JSON;//转换格式 默认json
	
	public MyFormat(){
		decrypt=new MyDecrypt();
	}
	
//	public MyFormat(MyDecrypt d){
//		decrypt=d;
//	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Class type = MyBean.class;
		System.out.println(type.getClassLoader() == null);

		MyFormat demo = new MyFormat();
		MyCoordinate my = new MyCoordinate();
		Map map = demo.toMap(my);
		MyUtils.show(map);

		System.out.println(my.getClass().getClassLoader() == null);
		String temp = demo.decrypt(my).toString();
		System.out.println(temp);
		Users user = new Users();

		List list = new ArrayList();
		list.add(new Users());
		list.add(new Users());
		user.setList(list);
		 temp = demo.decrypt(user);
		System.out.println(temp);
		System.out.println("^_^");

		// temp=String.format("%s,%s",new String[]{"html","table"} );
		list = new ArrayList();
		list.add("table");
		list.add("tr");
		temp = String.format("%s,%s", list.toArray());
		//temp=demo.toXML(user);
		System.out.println(temp);
	}

	/**
	 * 加密json即转换为bean
	 * 
	 * @param type
	 * @param json
	 * @return
	 */
	public <T> T encrypt(Class<T> type, String json) {
		return null;
	}

	/**
	 * 解密 bean转换为json
	 * 
	 * @param t
	 * @return
	 */
	public <T> String decrypt(T t) {
		return decrypt.toFormat(t).toString();// 未格式化处理
	}
	
	public <T> String decrypt(T t,String format) {
		Method function;
		try {
			function = decrypt.getClass().getMethod("to"+format);
			System.out.println(function.getName());
			function.invoke(decrypt,t).toString();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";// 未格式化处理
	}

	public static <T> Map toMap(final T t) {
		final Map map = new HashMap();
		// System.out.println(t);
		MyBean.visit(t, new Ports() {

			@Override
			public Object execute(Object obj) {
				// TODO Auto-generated method stub
				// if(obj instanceof PropertyDescriptor){
				PropertyDescriptor pd = (PropertyDescriptor) obj;
				Class type = pd.getPropertyType();
				String name = pd.getName();
//				if (name.equals("class")){
//					//types.put(t.getClass(), value);
//					return null;
//				}
				// System.out.println(pd.getName());
				try {
					map.put(pd.getName(), pd.getReadMethod().invoke(t));
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// }
				return null;
			}

		});
		return map;
	}

	
	
}
